// pages/mine/mine.js
const utils = require('../../utils/util.js');
const glob = getApp().globalData;
import {mySucscription,defaultPic} from "../../utils/imgbase64.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
      nickname:'',
      pic:'',
      hiddenModal:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.setData({
         mySucscription,
         defaultPic
     })
  },
  logout:function(){
      wx.showModal({
          title: '',
          content: '请您确定是否退出当前登录的多彩投账号',
          cancelColor:'#707070',
          confirmColor:'#0f2dcd',
          success: function (res) {
              if (res.confirm) {
                 wx.clearStorageSync();
                 wx.redirectTo({
                     url: '../views/login/login'
                 })
              } else if (res.cancel) {
                  console.log('取消')
              }
          }
      })
  },
  tapPic: function () {
      wx.showModal({
          title: '',
          content: '请在多彩投APP更换您的头像和昵称',
          confirmColor: '#0f2dcd',
          showCancel: false,
          success: function (res) {
              if (res.confirm) {
                  console.log('确定')
              }
          }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let app = getApp();
    app.loginState();
      let nickname = wx.getStorageSync('user_msg').nickname,
          pic = wx.getStorageSync('user_msg').avatar, 
          time = wx.getStorageSync('user_msg').timeout,
          nowtime = new Date().getTime(),
          uid = wx.getStorageSync('user_info').uid;
      if (utils.empty(nickname) && utils.empty(pic) && nowtime - Number(time)<=60000){
          this.setData({
              nickname:nickname,
              pic:pic
          })
      }else{
          utils.ajax(glob.ROOT_URL +'members/'+uid,{
              header:{
                  "Accept":'application/vnd.dct.v1+json'
              }
          },res=>{
             if(res.state=="1"){
                 let time = new Date().getTime();
                 let data = res.data;
                 data.timeout = time;
                 wx.setStorageSync('user_msg',data);
                 this.setData({
                     nickname: data.nickname,
                     pic: data.avatar
                 })
             }
          })
      }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
})